package com.czh;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;
import java.util.Scanner;

public class CodeGenApp {
    String username;

    String password;

    String url;
    final static Scanner scanner = new Scanner(System.in);

    String path;

    String  moduleName;

    String tableName;

    String propertiesPath;
    String driver;

    public static void main(String[] args) {
        CodeGenApp genApp = new CodeGenApp();
        setPropertiesPath(genApp);
        while (true){
            try {
                initProperties(genApp);
            } catch (IOException e) {
                System.out.println("路径名不太正确。。");
                continue;
            }
            break;
        }

        execute(genApp);
    }

    private static void execute(CodeGenApp genApp) {
        DataSource dat = DataSourceBuilder.create()
                .url(genApp.url)
                .password(genApp.password)
                .driverClassName(genApp.driver)
                .username(genApp.username)
                .build();
        FastAutoGenerator.create(new DataSourceConfig.Builder(dat))
                .globalConfig(builder -> {
                    builder.author("czh") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .commentDate("yyyy-MM-dd")
                            .outputDir(genApp.path + "java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(genApp.moduleName) // 设置父包名
//                            .moduleName("service") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml,genApp.path + "resources" + File.separator + "mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(genApp.tableName) //TODO 设置需要生成的表名
//                            .addTablePrefix("t_", "c_")
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation();
//                            .superClass("com.example.BaseEntity");//TODO not 父类
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    private static void initProperties(CodeGenApp genApp) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader(new File(genApp.propertiesPath)));
        genApp.path = properties.getProperty("czh.path");
        genApp.username = properties.getProperty("spring.datasource.username");
        genApp.password = properties.getProperty("spring.datasource.password");
        genApp.url = properties.getProperty("spring.datasource.url");
        genApp.moduleName = properties.getProperty("czh.module");
        genApp.tableName = properties.getProperty("czh.tableName");
        genApp.driver = properties.getProperty("spring.datasource.driver-class-name");
    }

    public static void setPropertiesPath(CodeGenApp genApp) {
        String name = "";
        String confirm;
        do {
            System.out.print("输入properties文件路径:");
            name = scanner.nextLine();
            System.out.println("确认路径--->" + name);
            System.out.println("OK?(enter/y  |  n)");
            confirm = scanner.nextLine();
            if (confirm.equals("") || confirm.equals("y")){
                genApp.propertiesPath = name;
                break;
            }
        }while (true);
    }
}
